﻿using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using umbraco.BusinessLogic;
using umbraco.DataLayer;
using umbraco.cms.businesslogic.datatype;


namespace idseefeld.com.images
{
    public class folderBrowserPngPreValue : System.Web.UI.WebControls.PlaceHolder, umbraco.interfaces.IDataPrevalue
    {

        private CheckBox _checkboxshowAllMediaTypes;
        private DropDownList _dropdownlist;
        // referenced datatype
        private umbraco.cms.businesslogic.datatype.BaseDataType _datatype;

        public static ISqlHelper SqlHelper
        {
            get { return Application.SqlHelper; }
        }

        public folderBrowserPngPreValue(umbraco.cms.businesslogic.datatype.BaseDataType DataType)
        {
            // state it knows its datatypedefinitionid
            _datatype = DataType;
            setupChildControls();

        }
        private void setupChildControls()
        {
            _dropdownlist = new DropDownList();
            _dropdownlist.ID = "dbtype";
            _dropdownlist.Items.Add(DBTypes.Date.ToString());
            _dropdownlist.Items.Add(DBTypes.Integer.ToString());
            _dropdownlist.Items.Add(DBTypes.Ntext.ToString());
            _dropdownlist.Items.Add(DBTypes.Nvarchar.ToString());


            _checkboxshowAllMediaTypes = new CheckBox();
            _checkboxshowAllMediaTypes.ID = "showAllMediaTypes";

            // put the childcontrols in context - ensuring that
            // the viewstate is persisted etc.
            this.Controls.Add(_dropdownlist);
            this.Controls.Add(_checkboxshowAllMediaTypes);

        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack)
            {
                string[] config = Configuration.Split("|".ToCharArray());
                if (config.Length > 0)
                {
                    _checkboxshowAllMediaTypes.Checked = config[0].Equals("1");
                }
                _dropdownlist.SelectedValue = _datatype.DBType.ToString();
            }
        }

        public Control Editor
        {
            get
            {

                return this;
            }
        }

        public void Save()
        {
            _datatype.DBType = (umbraco.cms.businesslogic.datatype.DBTypes)Enum.Parse(typeof(umbraco.cms.businesslogic.datatype.DBTypes), _dropdownlist.SelectedValue, true);

            // Generate data-string
            string data = _checkboxshowAllMediaTypes.Checked ? "1" : "0";
            // If the add new prevalue textbox is filled out - add the value to the collection.
            IParameter[] SqlParams = new IParameter[] {
										SqlHelper.CreateParameter("@value",data),
										SqlHelper.CreateParameter("@dtdefid",_datatype.DataTypeDefinitionId)};
            SqlHelper.ExecuteNonQuery("delete from cmsDataTypePreValues where datatypenodeid = @dtdefid", SqlParams);
            // need to unlock the parameters (for SQL CE compat)
            SqlParams = new IParameter[] {
										SqlHelper.CreateParameter("@value",data),
										SqlHelper.CreateParameter("@dtdefid",_datatype.DataTypeDefinitionId)};
            SqlHelper.ExecuteNonQuery("insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,0,'')", SqlParams);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteLine("<table>");
            writer.WriteLine("<tr><th>Database datatype</th><td>");
            _dropdownlist.RenderControl(writer);
            writer.Write("</td></tr>");
            writer.Write("<tr><th>Show all media types in overview:</th><td>");
            _checkboxshowAllMediaTypes.RenderControl(writer);
            writer.Write("</td></tr>");
            writer.Write("</table>");
        }

        public string Configuration
        {
            get
            {
                object configVal = SqlHelper.ExecuteScalar<object>("select value from cmsDataTypePreValues where datatypenodeid = @datatypenodeid", SqlHelper.CreateParameter("@datatypenodeid", _datatype.DataTypeDefinitionId));
                if (configVal != null)
                    return configVal.ToString();
                else
                    return "";
            }
        }
    }
}